package sdkshell.kuaiyan.com.kuaiyansdk.http.httpIpm;

import android.content.Context;

import java.util.HashMap;
import java.util.Map;

import sdkshell.kuaiyan.com.kuaiyansdk.http.Ihttp.NetConfig;
import sdkshell.kuaiyan.com.kuaiyansdk.http.enums.ProtrolType;
import sdkshell.kuaiyan.com.kuaiyansdk.http.enums.RequestType;

/**
 * Created by Administrator on 2016/7/11.
 */
public class NetConfigImp implements NetConfig {
    private boolean isImage = false;
    private String url = null;
    private RequestType type = RequestType.GET;
    private Map<String,String> heads = new HashMap<>();
    private Context context;
    private boolean onlyWify = false;
    private String content = null;
    private int currentTime = 0;
    private int retryTime = 3;
    private String encode;
    private boolean isDownload;
    private String downloadPath;
    private ProtrolType protrolType = ProtrolType.HTTP;
    public Map<String, String> getParams() {
        return params;
    }

    @Override
    public NetConfig addParamsAll(Map<String, String> params) {
        if(params!=null) {
            this.params.putAll(params);
        }
        return this;
    }

    @Override
    public NetConfig addParams(String key, String value) {
        params.put(key, value);
        return this;
    }

    @Override
    public NetConfig setOnlyWifiConnection(boolean onlyWifi) {
        this.onlyWify = onlyWifi;
        return this;
    }
    @Override
    public String getEncoding() {
        return encode;
    }

    @Override
    public NetConfig setEncoding(String encoding) {
        this.encode = encoding;
        return this;
    }

    @Override
    public NetConfig setProtrol(ProtrolType protrol) {
        this.protrolType = protrol;
        return this;
    }

    @Override
    public ProtrolType getProtrol() {
        return protrolType;
    }

    @Override
    public NetConfig setIsDownload(boolean download) {
        this.isDownload = download;
        return this;
    }

    @Override
    public boolean isDownload() {
        return isDownload;
    }

    @Override
    public String getDownloadPath() {
        return downloadPath;
    }

    @Override
    public NetConfig setDownloadPath(String downloadPath) {
        this.downloadPath = downloadPath;
        return this;
    }

    @Override
    public String getContent() {
        return content;
    }

    @Override
    public NetConfig setContent(String content) {
        this.content = content;
        return this;
    }

    @Override
    public boolean getIsOnlyWifyConnection() {
        return onlyWify;
    }

    @Override
    public NetConfig setRetry(int time) {
        this.retryTime = time;
        return this;
    }

    @Override
    public int getRetry() {
        return retryTime;
    }

    @Override
    public int getCurrentTryTime() {
        return currentTime;
    }

    @Override
    public NetConfig setCurrentTryTime(int time) {
        this.currentTime = time;
        return this;
    }

    @Override
    public boolean isImage() {
        return isImage;
    }

    @Override
    public NetConfig setIsImage(boolean isImage) {
        this.isImage = isImage;
        return this;
    }

    private Map<String,String> params = new HashMap<>();
    public RequestType getType() {
        return type;
    }
    @Override
    public NetConfig setType(RequestType type) {
        this.type = type;
        return this;
    }

    public Map<String, String> getHeads() {
        return heads;
    }
    @Override
    public NetConfig setHeads(Map<String, String> heads) {
        this.heads = heads;
        return this;
    }

    public NetConfigImp(final Context context){
        this.context = context;
    }

    @Override
    public NetConfig setUrl(String url) {
        this.url = url;
        if(url!=null) {
            if(url.toLowerCase().matches("\\s*http^s.*")){
                protrolType = ProtrolType.HTTP;
            }else  if(url.toLowerCase().matches("\\s*https.*")){
                protrolType = ProtrolType.HTTPS;
            }else{
                protrolType = ProtrolType.HTTP;
            }
        }
        return this;
    }

    @Override
    public String getUrl() {
        return url;
    }

    @Override
    public String getUrlAndParams() {
        return null;
    }

    @Override
    public NetConfig setHead(String key, String value){
        heads.put(key,value);
        return this;
    }
}
